package com.ymatou.iapi.optrade.parameter.vo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ymatou.iapi.optrade.parameter.utill.Utils;

public class OrderGroupVoForTax {

	private List<OrderVoForTax> orders;
	
	private long sellerId;
	
	private CouponVo sellerCoupon;
	
	private List<SellerPromotionVo> sellerPromotionApportions;
	
	// 订单组下各订单关于商家优惠券的权重
	private List<BigDecimal> orderWeightsOnSellerCoupon = new ArrayList<BigDecimal>();

	public List<OrderVoForTax> getOrders() {
		return orders;
	}

	public void setOrders(List<OrderVoForTax> orders) {
		this.orders = orders;
	}

	public long getSellerId() {
		return sellerId;
	}

	public void setSellerId(long sellerId) {
		this.sellerId = sellerId;
	}

	public CouponVo getSellerCoupon() {
		return sellerCoupon;
	}

	public void setSellerCoupon(CouponVo sellerCoupon) {
		this.sellerCoupon = sellerCoupon;
	}

	public List<SellerPromotionVo> getSellerPromotionApportions() {
		return sellerPromotionApportions;
	}

	public void setSellerPromotionApportions(
			List<SellerPromotionVo> sellerPromotionApportions) {
		this.sellerPromotionApportions = sellerPromotionApportions;
	}
	
	public List<BigDecimal> getOrderWeightsOnSellerCoupon() {
		return orderWeightsOnSellerCoupon;
	}

	public void setOrderWeightsOnSellerCoupon(
			List<BigDecimal> orderWeightsOnSellerCoupon) {
		this.orderWeightsOnSellerCoupon = orderWeightsOnSellerCoupon;
	}

	public BigDecimal[] calcMaxPromotionsLeft() {
		List<BigDecimal> result = new ArrayList<BigDecimal>();
		for (OrderVoForTax o : orders) {
			result.add(o.calcMaxPromotionLeft());
		}
		return result.toArray(new BigDecimal[0]);
	}
	
	/**
	 * 将商家优惠分配到各个订单项
	 */
	public void distributeSellerPromotionApportions() {
		if (this.getSellerPromotionApportions() != null) {
			for (SellerPromotionVo p : this
					.getSellerPromotionApportions()) {
				if (p.getQtyCondition() <= 0
						&& (p.getAmountCondition() == null || p
								.getAmountCondition()
								.compareTo(BigDecimal.ZERO) <= 0)) {
					throw new IllegalArgumentException("卖家优惠活动条件未提供:"
							+ p.getId());
				}

				//该优惠应用到订单项
				List<OrderItemApportionNum> itemApportions = new ArrayList<OrderItemApportionNum>();
				for (CatalogApportionVo c : p.getApportions()) {
					boolean matched = false;
					for (OrderVoForTax o : orders) {
						if (matched) {
							break;
						}
						for (OrderItemVoForTax item : o.getOrderItems()) {
							if (item.getCatalogId()
									.equals(c.getCatalogId())
									//还有数量可分摊优惠
									&& item.getProductNum()
											- item.countPromotionProductNum() >= c
												.getCount()) {
								item.getCatalogApportionVos().add(c);

								//一个商品同一时刻只享受一次优惠活动，各活动信息是一致的,选取最后一个
								item.setSellerPromotion(p);
								itemApportions.add(new OrderItemApportionNum(
										item, c.getCount()));

								matched = true;
								break;
							}
						}
					}
				}

				if (itemApportions.size() > 0) {
					// 订单项关于该优惠的权重
					List<BigDecimal> weights = new ArrayList<BigDecimal>();
					List<BigDecimal> maxLimits = new ArrayList<BigDecimal>();

					for (OrderItemApportionNum itemApportion : itemApportions) {
						weights.add(itemApportion.orderItem
								.getProductPrice()
								.multiply(
										new BigDecimal(
												itemApportion.apportionNum)));
						maxLimits.add(itemApportion.orderItem
								.calcMaxPromotionLeft());

					}

					BigDecimal[] apportions = Utils
							.apportionByWeightsAndMaxLimit(
									p.getReducedAmount(),
									weights.toArray(new BigDecimal[0]),
									maxLimits.toArray(new BigDecimal[0]), null);
					int i = 0;

					for (OrderItemApportionNum itemApportion : itemApportions) {
						if (apportions[i].compareTo(BigDecimal.ZERO) > 0) {
							itemApportion.orderItem
									.setSellerPromotionValue(Utils
											.zeroIfNull(
													itemApportion.orderItem
															.getSellerPromotionValue())
											.add(apportions[i]));
						}
						i++;
					}
				}

			}

			for (OrderVoForTax o : orders) {
				BigDecimal sum = BigDecimal.ZERO;

				for (OrderItemVoForTax item : o.getOrderItems()) {
					sum = sum.add(Utils.zeroIfNull(item
							.getSellerPromotionValue()));
				}

				o.setSellerPromotionAmount(sum);
			}
		}
	}

	private static class OrderItemApportionNum {
		
		public OrderItemVoForTax orderItem;
		public int apportionNum;
		
		public OrderItemApportionNum(OrderItemVoForTax orderItem, int apportionNum) {
			this.orderItem = orderItem;
			this.apportionNum = apportionNum;
		}
	}
}
